package ${package}.app.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 配置了这个就是异步事件监听了
 * Author:junru.pan
 * Email: panyiboy@gamil.com
 */
@Configuration
public class ExecutorConfig {

    /**
     * 异步线程池
     * @return
     */
    @Bean("threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor getTreadPoolTaskExecutor() {
        ThreadPoolTaskExecutor tp = new ThreadPoolTaskExecutor();
        tp.setCorePoolSize(10);
        tp.setMaxPoolSize(30);
        tp.setQueueCapacity(30);
        tp.setThreadNamePrefix("treadPoolTaskExecutor-");
        return tp;
    }

    /**
     * 自定义异步事件广播器
     * @param tp
     * @return
     */
    @Bean("applicationEventMulticaster")
    public SimpleApplicationEventMulticaster getSimpleApplicationEventMulticaster(
            @Qualifier("threadPoolTaskExecutor") ThreadPoolTaskExecutor tp){
        SimpleApplicationEventMulticaster sp = new SimpleApplicationEventMulticaster();
        sp.setTaskExecutor(tp);
        return sp;
    }
}